package cc.fozone.note.help;

import java.text.BreakIterator;

public class TextHelper {
	public static Boundary transferLine(String text, int start, int end) {
		if(text == null)
			return new Boundary(start,end);
		
		BreakIterator boundary = BreakIterator.getCharacterInstance();
		int length = text.length();
		boundary.setText(text);
		if(start == end) {
			if(end >= length) {
				start --;
			} else if (start <= 0) {
			} else {
				char c = text.charAt(end);
				if(c == '\n' || c == '\r') {
					start --;
				}
			}
		}
		for(;start >= 0 && start < length;start = boundary.preceding(start)) {
			char c = text.charAt(start);
			if(c == '\n' || c == '\r') {
				break;
			}
		}
		
		for(;end >= 0 && end < length;end = boundary.following(end)) {
			char c = text.charAt(end);
			if(c == '\n' || c == '\r') {
				break;
			}
		}
		if(start <= 0) end ++;
		start = start < 0 ? 0 : start;
		end = end > length ? length : end;
		
		return new Boundary(start, end);
		
	} 

}
